
/*
________________________________________________________________________________
Start by calculating parental income ranks
*/

* Income panel for all Finns 

use vuosi shnro svatv svatva svatvp kturaha using "D:\ready-made\FOLK_tulo_8800a\folk_19872000_tua_tulo21tot_1.dta" , clear
save "$data\99_temp\income_87-00.dta", replace

use vuosi shnro svatva svatvp kturaha svatv using "D:\ready-made\FOLK_tulo_0110a\folk_20012010_tua_tulo21tot_1.dta" , clear
save "$data\99_temp\income_01-10.dta", replace

use vuosi shnro svatva svatvp kturaha svatv using "D:\ready-made\FOLK_tulo_11a\folk_20112019_tua_tulo21tot_1.dta" , clear
save "$data\99_temp\income_11-19.dta", replace

use "$data\99_temp\income_11-19.dta",clear
append using "$data\99_temp\income_01-10.dta"
append using "$data\99_temp\income_87-00.dta"
save "$data\99_temp\income_87-19.dta", replace

erase "$data\99_temp\income_11-19.dta"
erase "$data\99_temp\income_87-00.dta"
erase "$data\99_temp\income_01-10.dta"

* Age panel for all Finns

use vuosi shnro ika using "D:\ready-made\FOLK_perus_8800a\folk_19872000_tua_perus21tot_1.dta" , clear
save "$data\99_temp\age_87-00.dta", replace

use vuosi shnro ika using "D:\ready-made\FOLK_perus_0110a\folk_20012010_tua_perus21tot_1.dta" , clear
save "$data\99_temp\age_01-10.dta", replace

use vuosi shnro ika using "D:\ready-made\FOLK_perus_11a\folk_20112020_tua_perus22tot_1.dta" , clear
drop if vuosi==2020
save "$data\99_temp\age_11-19.dta", replace

use "$data\99_temp\age_11-19.dta",clear
append using "$data\99_temp\age_01-10.dta"
append using "$data\99_temp\age_87-00.dta"
save "$data\99_temp\age_87-19.dta", replace

erase "$data\99_temp\age_11-19.dta"
erase "$data\99_temp\age_87-00.dta"
erase "$data\99_temp\age_01-10.dta"

* Parent-child links

use "D:\ready-made\FOLK_laps_70a\folk_19702020_tua_laps21_1.dta", clear

* drop if daddy and mommy are missing
drop if shnro_m=="" & shnro_f==""
drop folk*

* drop if individual dies before 2002
drop if kuolv<=2002
sort syntyv

preserve 
keep if shnro_f!=""
ren shnro shnro_kid
ren shnro_f shnro
drop shnro_m
gen dad=1
save "$data\99_temp\kids_dads.dta", replace
restore

preserve
keep if shnro_m!=""
ren shnro shnro_kid
ren shnro_m shnro
drop shnro_f
gen mom=1
save "$data\99_temp\kids_moms.dta", replace
restore

use "$data\99_temp\kids_dads.dta", clear
append using "$data\99_temp\kids_moms.dta"
sort shnro_kid syntyv
save "$data\99_temp\kids_parents.dta", replace

use "$data\99_temp\kids_parents.dta", clear
keep if dad==1
duplicates drop shnro, force
save "$data\99_temp\kids_dads_uniq.dta", replace

use "$data\99_temp\kids_parents.dta", clear
keep if mom==1
duplicates drop shnro, force
save "$data\99_temp\kids_moms_uniq.dta", replace


use "$data\99_temp\income_87-19.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\age_87-19.dta"
drop _merge

egen anstu=rowtotal(svatv svatva svatvp)
egen mktinc=rowtotal(svatva svatvp)

* Age restriction 46-50

preserve
keep if ika>45 & ika<=50
merge m:1 shnro using "$data\99_temp\kids_dads_uniq.dta"
keep if dad==1
bys shnro: egen mean_inc_dad=mean(anstu) 
xtile ptile_dad = mean_inc_dad, nq(100)
bys shnro: egen mean_capinc_dad=mean(svatvp)  if vuosi>1992
xtile ptile_cap_dad = mean_capinc_dad if vuosi>1992, nq(100) 
bys shnro: egen mean_di_dad=mean(kturaha) if vuosi>1994
xtile ptile_di_dad = mean_di_dad if vuosi>1994, nq(100)
save "$data\99_temp\inc_kids_dads.dta", replace
restore

preserve
keep if ika>45 & ika<=50
merge m:1 shnro using "$data\99_temp\kids_moms_uniq.dta"
keep if mom==1
bys shnro: egen mean_inc_mom=mean(anstu) 
xtile ptile_mom = mean_inc_mom, nq(100)
bys shnro: egen mean_capinc_mom=mean(svatvp) if vuosi>1992
xtile ptile_cap_mom = mean_capinc_mom if vuosi>1992, nq(100)
bys shnro: egen mean_di_mom=mean(kturaha) if vuosi>1994
xtile ptile_di_mom = mean_di_mom if vuosi>1994, nq(100)
save "$data\99_temp\inc_kids_moms.dta", replace
restore

use "$data\99_temp\inc_kids_dads.dta", clear
append using "$data\99_temp\inc_kids_moms.dta"
drop if vuosi==.
keep shnro* syntyv kuolv dad mom mean* ptile* vuosi
duplicates drop shnro, force
save "$data\99_temp\inc_kids_dads_moms.dta", replace

use "$data\99_temp\kids_parents.dta", clear
duplicates drop shnro shnro_kid, force
merge m:m shnro using "$data\99_temp\inc_kids_dads_moms.dta"
drop if _merge==1
sort shnro_kid 
* market income
gen parent_inc=mean_inc_dad
replace parent_inc=mean_inc_mom if mom==1
bys shnro_kid: egen total_parent_inc=sum(parent_inc)
xtile ptile = total_parent_inc, nq(100)

* cap inc
preserve
drop if vuosi<1992
gen parent_capinc=mean_capinc_dad
replace parent_capinc=mean_capinc_mom if mom==1
bys shnro_kid: egen total_parent_capinc=sum(parent_capinc)
xtile ptile_ci = total_parent_capinc, nq(100)
tab ptile_ci
bys shnro_kid: egen count=count(syntyv)
gen only_dad=1 if count==1 & dad==1
gen only_mom=1 if count==1 & mom==1
* bys shnro_kid: egen ptile_Dad=min(ptile_dad)
* bys shnro_kid: egen ptile_Mom=min(ptile_mom)
* drop ptile_dad ptile_mom
keep shnro* ptile* only* parent*
duplicates drop shnro_kid, force
keep shnro_kid ptile* only* parent*
ren shnro_kid shnro
save "$data\02_parents\ptile_parent_capinc.dta", replace
restore 

* dispinc
preserve
drop if vuosi<1995
gen parent_di=mean_di_dad
replace parent_di=mean_di_mom if mom==1
bys shnro_kid: egen total_parent_di=sum(parent_di)
xtile ptile_di = total_parent_di, nq(100)
tab ptile_di
bys shnro_kid: egen count=count(syntyv)
gen only_dad=1 if count==1 & dad==1
gen only_mom=1 if count==1 & mom==1
* bys shnro_kid: egen ptile_Dad=min(ptile_dad)
* bys shnro_kid: egen ptile_Mom=min(ptile_mom)
* drop ptile_dad ptile_mom
keep shnro* ptile* only* parent*
duplicates drop shnro_kid, force
keep shnro_kid ptile* only* parent*
ren shnro_kid shnro

save "$data\02_parents\ptile_parent_diinc.dta", replace
restore

bys shnro_kid: egen count=count(syntyv)
gen only_dad=1 if count==1 & dad==1
gen only_mom=1 if count==1 & mom==1
* bys shnro_kid: egen ptile_Dad=min(ptile_dad)
* bys shnro_kid: egen ptile_Mom=min(ptile_mom)
* drop ptile_dad ptile_mom
keep shnro* ptile* only* parent*
duplicates drop shnro_kid, force
keep shnro_kid ptile* only* parent*
ren shnro_kid shnro

save "$data\02_parents\ptile_parent.dta", replace

erase "$data\99_temp\age_87-19.dta"
erase "$data\99_temp\income_87-19.dta"
erase "$data\99_temp\inc_kids_dads.dta"
erase "$data\99_temp\inc_kids_moms.dta"
erase "$data\99_temp\inc_kids_dads_moms.dta"
erase "$data\99_temp\kids_dads.dta"
erase "$data\99_temp\kids_moms.dta"
erase "$data\99_temp\kids_dads_uniq.dta"
erase "$data\99_temp\kids_moms_uniq.dta"
erase "$data\99_temp\kids_parents.dta"









/*
________________________________________________________________________________
FOLK panel
*/

* FOLK income

use vuosi shnro saatusi makstu kturaha if vuosi>=1995 using "D:\ready-made\FOLK_tulo_8800a\folk_19872000_tua_tulo21tot_1.dta", clear
append using "D:\ready-made\FOLK_tulo_0110a\folk_20012010_tua_tulo21tot_1.dta", keep(vuosi shnro saatusi makstu kturaha)
append using "D:\ready-made\FOLK_tulo_11a\folk_20112019_tua_tulo21tot_1.dta", keep(vuosi shnro saatusi makstu kturaha)
foreach var of varlist saatusi makstu kturaha {
	replace `var'=0 if `var'==.
}
save "$data\01_main\folk_flown_merge.dta", replace

* Narrow the sample from the very start: require info on parental income

merge m:1 shnro using "W:\Inequality\2021rnd2\Data\ptile_parents.dta", keep(3) keepusing(ptile)
drop _merge
rename ptile ptile_prnt
label var ptile_prnt "Parental income rank (both parents, 46-50 yo)"

* FOLK other variables

preserve
use vuosi shnro sukup kansa1_k syntyp2 kieli_k ika kunta sivs sose yotutk ututku_aste ututku_ala suorv amas1 optuki ptoim1 ammattikoodi_k tyke lkm_k if vuosi>=1995 using "D:\ready-made\FOLK_perus_8800a\folk_19872000_tua_perus20tot_2.dta", clear
append using "D:\ready-made\FOLK_perus_0110a\folk_20012010_tua_perus20tot_2.dta", keep(vuosi shnro sukup kansa1_k syntyp2 kieli_k ika kunta sivs sose yotutk ututku_aste ututku_ala suorv amas1 optuki ptoim1 ammattikoodi_k tyke lkm_k)
append using "D:\ready-made\FOLK_perus_11a\folk_20112019_tua_perus20tot_2.dta", keep(vuosi shnro sukup kansa1_k syntyp2 kieli_k ika kunta sivs sose yotutk ututku_aste ututku_ala suorv amas1 optuki ptoim1 ammattikoodi_k tyke lkm_k)
save "$data\99_temp\folk_perus_9719.dta", replace
restore
merge 1:1 vuosi shnro using "$data\99_temp\folk_perus_9719.dta", keep(3)
drop _merge
erase "$data\99_temp\folk_perus_9719.dta"
order shnro vuosi
sort shnro vuosi

* Calculate market income and rename kturaha to disposable income

gen mkti=.
gen dspi=.
rename kturaha dspi_nom
gen mkti_nom = dspi_nom + makstu - saatusi
foreach var of varlist mkti dspi {
	replace `var' = `var'_nom / 0.8680 if vuosi==1995
	replace `var' = `var'_nom / 0.8730 if vuosi==1996
	replace `var' = `var'_nom / 0.8840 if vuosi==1997
	replace `var' = `var'_nom / 0.8960 if vuosi==1998
	replace `var' = `var'_nom / 0.9070 if vuosi==1999
	replace `var' = `var'_nom / 0.9340 if vuosi==2000
	replace `var' = `var'_nom / 0.9590 if vuosi==2001
	replace `var' = `var'_nom / 0.9780 if vuosi==2002
	replace `var' = `var'_nom / 0.9910 if vuosi==2003
	replace `var' = `var'_nom / 0.9920 if vuosi==2004
	replace `var' = `var'_nom if vuosi==2005
	replace `var' = `var'_nom / 1.0130 if vuosi==2006
	replace `var' = `var'_nom / 1.0290 if vuosi==2007
	replace `var' = `var'_nom / 1.0690 if vuosi==2008
	replace `var' = `var'_nom / 1.0870 if vuosi==2009
	replace `var' = `var'_nom / 1.1050 if vuosi==2010
	replace `var' = `var'_nom / 1.1420 if vuosi==2011
	replace `var' = `var'_nom / 1.1780 if vuosi==2012
	replace `var' = `var'_nom / 1.2037 if vuosi==2013
	replace `var' = `var'_nom / 1.2183 if vuosi==2014
	replace `var' = `var'_nom / 1.2164 if vuosi==2015
	replace `var' = `var'_nom / 1.2211 if vuosi==2016
	replace `var' = `var'_nom / 1.2313 if vuosi==2017
	replace `var' = `var'_nom / 1.2457 if vuosi==2018
	replace `var' = `var'_nom / 1.2599 if vuosi==2019
	*replace `var' = `var'_nom / 1.2647 if vuosi==2020
}
order shnro vuosi dspi mkti
drop saatusi makstu dspi_nom mkti_nom //TYHHYV
label var dspi "Disposable income"
label var mkti "Market income"
foreach var of varlist dspi mkti {
	replace `var'=0 if `var'==.
}
sort shnro vuosi

* Controls

gen ikasq = ika*ika
gen pks=0
replace pks=1 if kunta=="049" | kunta=="091" | kunta=="235" | kunta=="092"
destring sukup, replace
destring ututku_aste, replace
destring sivs, replace
gen highed=.
replace highed=0 if ututku_aste==3
replace highed=1 if ututku_aste==4 | ututku_aste==5 // secondary
replace highed=2 if ututku_aste==6 | ututku_aste==7 | ututku_aste==8 // bachelor or higher
replace highed=99 if highed==.
gen married=0
replace married=1 if sivs==2
replace lkm_k=0 if lkm_k==. // nr of children
destring sukup, replace
replace sukup = sukup - 1
keep shnro-ika sose amas1 ptoim1-married
order shnro-sukup ika ikasq pks highed married lkm_k
label var shnro "Person id"
label var vuosi "Year"
label var sukup "Gender (0=male)"
label var ika "Age"
label var ikasq "Age squared"
label var pks "Capital region"
label var highed "Education (3 categories)"
label var married "Married"
label var lkm_k "Number of children"
label var kansa1_k "Citizenship"
label var syntyp2 "Birth origin"
label var kieli_k "Language"
label var sose "Socio-economic status (StatFin definition)"
label var amas1 "Occupation group"
label var ptoim1 "Main activity"
label var ammattikoodi_k "Occupation"
label var tyke "Months unemployed"

save "$data\01_main\folk_flown_merge.dta", replace
clear